package b3.widget.warehouse
{
	import b3.config.*;
	import b3.model.*;
	import b3.net.*;
	import b3.scene.home.HomeScene;
	import b3.scene.build.BuildScene;
	import b3.scene.SceneManager;
	import b3.scene.home.Town;
	import b3.util.Reflect;
	import b3.widget.WidgetManager;
	import b3.widget.basic.PopMessage;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;

	public class PermitItem extends ObjectItem
	{
		private var buildingIndex:int;
		private var roofType:int;
		private var upgrade:int;
		private var imgContainer:Sprite;
		
		public function PermitItem(obj:Obj)
		{
			var permitConfig:PermitConfig = Config.objConfigs[obj.objectIndex];
			this.buildingIndex = permitConfig.buildingIndex;
			this.roofType = permitConfig.roofType;
			this.upgrade = permitConfig.upgradeType;
			super(obj);
		
		}
		
		protected override function onMouseClick(event:MouseEvent):void
		{
			if (HomeScene.getInstance().getOperationMode() != HomeScene.NONEMODE)
				return;
			var popMessage:PopMessage;
			var permitConfig:PermitConfig = Config.objConfigs[obj.objectIndex];
			var buildingConfig:BuildingConfig = Config.buildingConfigs[permitConfig.buildingIndex];
			var me:Player = PlayerManager.getInstance().me;
			if (HomeScene.getInstance().getCurrentTown().owner.userId != PlayerManager.getInstance().myUserId)
			{
				popMessage = new PopMessage("不能在别人的家里进行建设！", 0xFF0000);
				WidgetManager.getInstance().popOut([popMessage]);
			}
			else if (buildingConfig.vitalityCost > me.vitality)
			{
				popMessage = new PopMessage("体力不足！", 0xFF0000);
				WidgetManager.getInstance().popOut([popMessage]);
			}
			else
			{
				var town:Town = HomeScene.getInstance().getCurrentTown();
				if (town.owner.townModel.getAvailablePoints(permitConfig.buildingIndex).length > 0)
				{
					
					var objBalance:Obj = new Obj();
					objBalance.objectIndex = obj.objectIndex;
					objBalance.objectNum = -1;
					PlayerManager.getInstance().me.checkAndBuild(objBalance);
					PlayerManager.getInstance().me.addEventListener(ModelEvent.CHECK_AND_BUILD_COMPLETE, onCheckAndBuildComplete);
				}
				else
				{
					popMessage = new PopMessage("当前没有合适的地块建造！", 0xFF0000);
					WidgetManager.getInstance().popOut([popMessage]);
				}
			}
		}
		
		private function onCheckAndBuildComplete( event:ModelEvent ):void
		{
			var me:Player = PlayerManager.getInstance().me;
			me.removeEventListener(ModelEvent.CHECK_AND_BUILD_COMPLETE, onCheckAndBuildComplete);
			var data:Object = event.data;
			if (data['err'] == 0)
			{
				//关闭包裹
			 	Warehouse.getInstance().close();
				var oldState:StateModel = new StateModel(me.userId);
				oldState.handleLoadStates(data['oldState']);
			 	SceneManager.getInstance().loadScene(BuildScene.getInstance());
			 	BuildScene.getInstance().changeParamByState(oldState);
			 	BuildScene.getInstance().startBuild(buildingIndex, me.maxHeight(buildingIndex), me.maxRoofType(buildingIndex));
			}
			else
			{
				var popMessage:PopMessage = new PopMessage("物品不足！", 0xFF0000);
				WidgetManager.getInstance().popOut([popMessage]);
			}		
		}
		
		protected override function onMouseOver(event:MouseEvent):void
		{
			super.onMouseOver(event);
//			if (HomeScene.getInstance() == PlayerManager.CENTER_TYPE)
//				return;
//			var highlightColor:uint = false ? 0xFF0000 : 0x00FF00;
//			var town:Town = HomeScene.getInstance().getCurrentTown();
//			town.addHighLight(buildingIndex);
			
//			//鼠标信息
//			var globalPoint:Point = this.parent.localToGlobal(new Point(this.x, this.y));
//			var buildingTip:BuildingMessage = new BuildingMessage(buildingIndex); 
//			buildingTip.x = globalPoint.x - (buildingTip.width - width) / 2;
//			buildingTip.y = globalPoint.y - buildingTip.height - 5;
//			WidgetManager.getInstance().popOutMouseOverMessage(buildingTip);
		}
	}
}